define(['jquery', 'bootstrap', 'frontend', 'form','util'], function ($, undefined, Frontend, Form,Util) {
    var validatoroptions = {
        invalid: function (form, errors) {
            $.each(errors, function (i, j) {
                Layer.msg(j);
            });
        }
    };
    var Controller = {
        login: function () {
            //本地验证未通过时提示
            $("#login-form").data("validator-options", validatoroptions);

            $(document).on("change", "input[name=type]", function () {
                var type = $(this).val();
                $("div.form-group[data-type]").addClass("hide");
                $("div.form-group[data-type='" + type + "']").removeClass("hide");
                $('#resetpwd-form').validator("setField", {
                    captcha: "required;length(4);integer[+];remote(" + $(this).data("check-url") + ", event=resetpwd, " + type + ":#" + type + ")",
                });
                $(".btn-captcha").data("url", $(this).data("send-url")).data("type", type);
            });

            //为表单绑定事件
            Form.api.bindevent($("#login-form"), function (data, ret) {
                setTimeout(function () {
                    location.href = ret.url ? ret.url : "/";
                }, 1000);
            });

            Form.api.bindevent($("#resetpwd-form"), function (data) {
                Layer.closeAll();
            });

            $(document).on("click", ".btn-forgot", function () {
                var id = "resetpwdtpl";
                var content = Template(id, {});
                Layer.open({
                    type: 1,
                    title: __('Reset password'),
                    area: ["450px", "355px"],
                    content: content,
                    success: function (layero) {
                        Form.api.bindevent($("#resetpwd-form", layero), function (data) {
                            Layer.closeAll();
                        });
                    }
                });
            });
        },

        feedback: function () {
            require(['upload'], function (Upload) {
                Upload.api.plupload('#btn-upload', function(data) {
                    var url =  data.url;//不带http的路径
                    $('.upload').before('<div class="img_box"><input type="hidden" value="'+url+'" name="images[]"><img src="'+Fast.api.cdnurl(url)+'" alt=""><i class="Yicon icon_close"></i></div>');
                },function(data, error){
                    var msgs = {
                        '-600': '文件最大2M',
                    };
                    var msg = msgs[error.code] || error.msg;
                    Toastr.error(msg);
                    return false;
                });
            });
            $('#images').on('click', '.icon_close', function () {
                $(this).closest('.img_box').remove();
            });
            $('#tags').find('.tag').on('click', function () {
                $(this).addClass('active').find('input:radio').prop('checked',true);
                $(this).siblings('.tag').removeClass('active').find('input:radio').prop('checked',false);
            });

            Form.api.bindevent('#form', function (data, ret) {
                setTimeout(function () {
                    location.reload();
                },1000)
            });
        },
        api: {

        }
    };
    return Controller;
});
